package BandB.Tool.GPhoneManager.Widget.AirplaneModeSwitcher;

import BandB.Tool.GPhoneManager.R;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.util.Log;
import android.widget.RemoteViews;

public class AirplaneModeWidget extends AppWidgetProvider {
	private final String TAG = AirplaneModeWidget.class.getSimpleName();

	// public static RemoteViews views;
	// public static AppWidgetManager mAppWidgetManager;
	public static int[] mAppWidgetIds;

	// wifi status icon
	private final int RES_ID_TURN_ON = R.drawable.airplane3;
	private final int RES_ID_TURN_OFF = R.drawable.airplane;
	private final int RES_ID_TURNING_ON = R.drawable.airplane1;
	private final int RES_ID_TURNING_OFF = R.drawable.airplane2;

	public Context mContext;

	// bluetooth statuc icon

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		Log.i(TAG,
				"************AirplaneModeWidget widget on update*************");
		// mAppWidgetManager = appWidgetManager;
		mAppWidgetIds = appWidgetIds;
		// views = new RemoteViews(context.getPackageName(), R.layout.widget);
		mContext = context;

		initAirplaneModeSwitcher(context);
		// com.mgeek.android.ads.UpdateService.startService(context);

	}
	private void initAirplaneModeSwitcher(Context context) {

		RemoteViews views = new RemoteViews(context.getPackageName(),
				R.layout.airplane_widget);
		Intent intent = new Intent(context, AirplaneModeSettingService.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		PendingIntent pendingIntent = PendingIntent.getService(context, 0,
				intent, PendingIntent.FLAG_UPDATE_CURRENT);
		views.setOnClickPendingIntent(R.id.airplane_mode_switch_button,
				pendingIntent);

		boolean isEnabled = Settings.System.getInt(
				context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
				0) == 1;

		if (isEnabled) {
			Log.i(TAG, "airplane mode is enabled");
			updateAirplaneModeSwitcher(context, RES_ID_TURN_ON);
		} else {
			Log.i(TAG, "airplane mode is disabled");
			updateAirplaneModeSwitcher(context, RES_ID_TURN_OFF);
		}

		// ConnectivityManager cm =
		// (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
		// NetworkInfo networkInfo = cm.getActiveNetworkInfo();
		// if(null != networkInfo){
		// Log.i(TAG, "networkInfo is not null");
		// updateAirplaneModeSwitcher(RES_ID_TURN_OFF);
		// }else{
		// Log.i(TAG, "networkInfo is null");
		//			
		// }
	}

	private void updateAirplaneModeSwitcher(Context context, int resId) {

		RemoteViews views = new RemoteViews(context.getPackageName(),
				R.layout.airplane_widget);
		AppWidgetManager mAppWidgetManager = AppWidgetManager
				.getInstance(context);
		if (null != views && null != mAppWidgetManager) {

			Intent intent = new Intent(context,
					AirplaneModeSettingService.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			PendingIntent pendingIntent = PendingIntent.getService(context, 0,
					intent, PendingIntent.FLAG_UPDATE_CURRENT);
			views.setOnClickPendingIntent(R.id.airplane_mode_switch_button,
					pendingIntent);
			views.setImageViewResource(R.id.airplane_mode_status, resId);
			mAppWidgetManager.updateAppWidget(new ComponentName(context,
					AirplaneModeWidget.class), views);
		} else {
			// views is null,we need to send an intent to update views
			Log.i(TAG, "views == null || mAppWidgetManager == null");
		}

	}

	// onreceive invoked before onUpdate()
	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		Log.i(TAG, "action 110=" + action);
		// if we send broadcast byself
		if (action.equals("android.intent.action.SERVICE_STATE")) {
			boolean isEnabled = Settings.System.getInt(context
					.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1;
			if (isEnabled) {
				Log.i(TAG, "airplane mode is enabled");
				updateAirplaneModeSwitcher(context, RES_ID_TURN_ON);
			} else {
				Log.i(TAG, "airplane mode is disabled");
				updateAirplaneModeSwitcher(context, RES_ID_TURN_OFF);
			}
		} else if (action
				.equals(AirplaneModeSettingService.ACTION_AIRPLANE_MODE_STATE)) {
			int processingState = intent.getExtras().getInt(
					AirplaneModeSettingService.KEY_PROCEING_STATE);
			if (processingState == AirplaneModeSettingService.PROCESSING_STATE_TURNING_ON) {
				updateAirplaneModeSwitcher(context, RES_ID_TURNING_ON);
			} else if (processingState == AirplaneModeSettingService.PROCESSING_STATE_TURNING_OFF) {
				updateAirplaneModeSwitcher(context, RES_ID_TURNING_OFF);
			}
		}

		super.onReceive(context, intent);
	}

}
